package my.edu.model.bills.bound.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import my.edu.model.bills.bound.entity.OtherInboundOrderDetail;
import my.edu.model.bills.bound.entity.OtherOutboundOrder;
import my.edu.model.bills.bound.entity.dto.InboundDetailDTO;
import my.edu.model.bills.bound.entity.dto.OutboundDetailDTO;
import my.edu.model.commodity.service.IProductService;
import my.edu.utils.OrderServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import my.edu.model.bills.bound.entity.OtherOutboundOrderDetail;
import my.edu.model.bills.bound.mapper.OtherOutboundOrderDetailMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OtherOutboundOrderDetailServiceImpl extends ServiceImpl<OtherOutboundOrderDetailMapper, OtherOutboundOrderDetail> implements IOtherOutboundOrderDetailService{

    @Autowired
    private IProductService productService;

    @Autowired
    private OrderServiceUtil orderServiceUtil;

    @Override
    @Transactional
    public void saveByDTO(List<OutboundDetailDTO> outboundDetailDTOS, OtherOutboundOrder otherOutboundOrder) {
        for (OutboundDetailDTO outboundDetailDTO : outboundDetailDTOS) {
            productService.checkProduct(outboundDetailDTO.getProductId());
        }
        List<OtherOutboundOrderDetail> list = outboundDetailDTOS.stream()
                .peek(i -> {
                    productService.checkProduct(i.getProductId());
                })
                .map(i -> {
                    OtherOutboundOrderDetail otherOutboundOrderDetail = BeanUtil.copyProperties(i, OtherOutboundOrderDetail.class);
                    otherOutboundOrderDetail.setOtherOutboundOrderId(otherOutboundOrder.getId());
                    return otherOutboundOrderDetail;
                }).toList();
        saveBatch(list);
    }

    @Override
    @Transactional
    public void updateByDTO(List<OutboundDetailDTO> outboundDetailDTOS, OtherOutboundOrder byId) {
        remove(Wrappers.<OtherOutboundOrderDetail>lambdaQuery().eq(OtherOutboundOrderDetail::getOtherOutboundOrderId, byId.getId()));
        saveByDTO(outboundDetailDTOS, byId);


    }

    @Override
    public void removeByOrderId(List<Long> longs) {
        remove(Wrappers.<OtherOutboundOrderDetail>lambdaQuery().in(OtherOutboundOrderDetail::getOtherOutboundOrderId, longs));
    }

    @Override
    public void subStockByOrderId(List<Long> longs) {
        //查询所有单据详情
        Map<Long, Integer> collect =  getDetailAndGetIdToQuantity(longs);
        productService.subCurrentStock(collect);

    }

    @Override
    public void addStockByOrderId(List<Long> longs) {
        //查询所有单据详情
        Map<Long, Integer> collect = getDetailAndGetIdToQuantity(longs);
        //增加库存
        productService.addCurrentStock(collect);
    }

    private Map<Long, Integer> getDetailAndGetIdToQuantity(List<Long> longs) {
        List<OtherOutboundOrderDetail> list=  getDetailByOrderIds(longs);
        return list.stream()
                .peek(i -> {productService.checkProduct(i.getProductId());})
                .collect(Collectors.groupingBy(
                        OtherOutboundOrderDetail::getProductId,
                        Collectors.summingInt(OtherOutboundOrderDetail::getQuantity)
                ));
    }

    private List<OtherOutboundOrderDetail> getDetailByOrderIds(List<Long> longs) {
        return list(Wrappers.<OtherOutboundOrderDetail>lambdaQuery().in(OtherOutboundOrderDetail::getOtherOutboundOrderId, longs));
    }

}
